package com.example.dialog.Tools;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.dialog.model.Post;

import java.util.ArrayList;
import java.util.List;

public class DatabaseHelperForFindItems extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "searchdatabase.db";    //数据库的名字
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelperForFindItems(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        String CREATE_TABLE = "CREATE TABLE searchdatabase ("   //改文件名点②
//                + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
//                + "searchText TEXT)";
//        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public List<Post> queryLike(){
        List<Post> searchResultList = new ArrayList<>(); //定义一个接收你要的数据结果的列表
        String selectQuery = "SELECT * FROM articleinfo where type like '%L%'";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                String mainImage = cursor.getString(cursor.getColumnIndex("mainImage"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String usrImage = cursor.getString(cursor.getColumnIndex("usrImage"));
                String usrName = cursor.getString(cursor.getColumnIndex("usrName"));
                String mtv_summary = cursor.getString(cursor.getColumnIndex("mtv_summary"));
                String contentImage = cursor.getString(cursor.getColumnIndex("contentImage"));
                String mtv_content = cursor.getString(cursor.getColumnIndex("mtv_content"));
                String mtv_data_read = cursor.getString(cursor.getColumnIndex("mtv_data_read"));
                String mtv_data_like = cursor.getString(cursor.getColumnIndex("mtv_data_like"));
                String mtv_data_star = cursor.getString(cursor.getColumnIndex("mtv_data_star"));
                Post post = new Post(mtv_summary, contentImage, mainImage, usrImage, usrName, title, mtv_data_like, mtv_data_read, mtv_data_star,mtv_content);
                searchResultList.add(post);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return searchResultList;
    }

    public List<Post> queryMy(){
        List<Post> searchResultList = new ArrayList<>(); //定义一个接收你要的数据结果的列表
        String selectQuery = "SELECT * FROM articleinfo where type like '%M%'";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                String mainImage = cursor.getString(cursor.getColumnIndex("mainImage"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String usrImage = cursor.getString(cursor.getColumnIndex("usrImage"));
                String usrName = cursor.getString(cursor.getColumnIndex("usrName"));
                String mtv_summary = cursor.getString(cursor.getColumnIndex("mtv_summary"));
                String contentImage = cursor.getString(cursor.getColumnIndex("contentImage"));
                String mtv_content = cursor.getString(cursor.getColumnIndex("mtv_content"));
                String mtv_data_read = cursor.getString(cursor.getColumnIndex("mtv_data_read"));
                String mtv_data_like = cursor.getString(cursor.getColumnIndex("mtv_data_like"));
                String mtv_data_star = cursor.getString(cursor.getColumnIndex("mtv_data_star"));
                Post post = new Post(mtv_summary, contentImage, mainImage, usrImage, usrName, title, mtv_data_like, mtv_data_read, mtv_data_star,mtv_content);
                searchResultList.add(post);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return searchResultList;
    }

    public List<Post> queryFollow(){
        List<Post> searchResultList = new ArrayList<>(); //定义一个接收你要的数据结果的列表
        String selectQuery = "SELECT * FROM articleinfo where type like '%F%'";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                String mainImage = cursor.getString(cursor.getColumnIndex("mainImage"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String usrImage = cursor.getString(cursor.getColumnIndex("usrImage"));
                String usrName = cursor.getString(cursor.getColumnIndex("usrName"));
                String mtv_summary = cursor.getString(cursor.getColumnIndex("mtv_summary"));
                String contentImage = cursor.getString(cursor.getColumnIndex("contentImage"));
                String mtv_content = cursor.getString(cursor.getColumnIndex("mtv_content"));
                String mtv_data_read = cursor.getString(cursor.getColumnIndex("mtv_data_read"));
                String mtv_data_like = cursor.getString(cursor.getColumnIndex("mtv_data_like"));
                String mtv_data_star = cursor.getString(cursor.getColumnIndex("mtv_data_star"));
                Post post = new Post(mtv_summary, contentImage, mainImage, usrImage, usrName, title, mtv_data_like, mtv_data_read, mtv_data_star,mtv_content);
                searchResultList.add(post);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return searchResultList;
    }

    public List<Post> querySearchDataBase() {    //sql查询函数
        List<Post> searchResultList = new ArrayList<>(); //定义一个接收你要的数据结果的列表
        String selectQuery = "SELECT * FROM articleinfo"; // 确保查询语句正确

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                String mainImage = cursor.getString(cursor.getColumnIndex("mainImage"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String usrImage = cursor.getString(cursor.getColumnIndex("usrImage"));
                String usrName = cursor.getString(cursor.getColumnIndex("usrName"));
                String mtv_summary = cursor.getString(cursor.getColumnIndex("mtv_summary"));
                String contentImage = cursor.getString(cursor.getColumnIndex("contentImage"));
                String mtv_content = cursor.getString(cursor.getColumnIndex("mtv_content"));
                String mtv_data_read = cursor.getString(cursor.getColumnIndex("mtv_data_read"));
                String mtv_data_like = cursor.getString(cursor.getColumnIndex("mtv_data_like"));
                String mtv_data_star = cursor.getString(cursor.getColumnIndex("mtv_data_star"));
                Post post = new Post(mtv_summary, contentImage, mainImage, usrImage, usrName, title, mtv_data_like, mtv_data_read, mtv_data_star,mtv_content);
                searchResultList.add(post);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return searchResultList;
    }

}
